import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.*;

public class POIDemo {
    @Test
    public void test1(){
        try {
            XSSFWorkbook execl =new XSSFWorkbook(new FileInputStream("d:\\aaa.xlsx"));
            XSSFSheet sheet = execl.getSheetAt(0);
            for (Row row : sheet) {
                for (Cell cell : row) {
                    System.out.println(cell.getStringCellValue());
                }
            }
            execl.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2(){
        try {
            XSSFWorkbook execl =new XSSFWorkbook(new FileInputStream("d:\\aaa.xlsx"));
            XSSFSheet sheet = execl.getSheetAt(0);
            int lastRowNum = sheet.getLastRowNum();
            for (int i = 0; i <=lastRowNum ; i++) {
                XSSFRow row = sheet.getRow(i);
                short lastCellNum = row.getLastCellNum();
                for (int j = 0; j <lastCellNum ; j++) {
                    XSSFCell cell = row.getCell(j);
                    String cellValue = cell.getStringCellValue();
                    System.out.println(cellValue);
                }
            }
            execl.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    @Test
    public void test3() throws IOException {
        XSSFWorkbook execl=new XSSFWorkbook();
        XSSFSheet sheet = execl.createSheet("朋友");
        XSSFRow row = sheet.createRow(0);
        XSSFCell cell = row.createCell(0);
        cell.setCellValue("姓名");
        XSSFCell cell2 = row.createCell(1);
        cell2.setCellValue("感觉");
        XSSFRow row2 = sheet.createRow(1);
        XSSFCell cell3 = row2.createCell(0);
        cell3.setCellValue("余思芸");
        XSSFCell cell4= row2.createCell(1);
        cell4.setCellValue("不错");
        FileOutputStream fileOutputStream = new FileOutputStream(new File("d:\\bbb.xlsx"));
        execl.write(fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
        execl.close();
    }
}
